package com.szsh.aiot.hsm.controller;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.szsh.aiot.common.annotation.CustomPermission;
import com.szsh.aiot.common.annotation.Log;
import com.szsh.aiot.common.core.domain.AjaxResult;
import com.szsh.aiot.common.core.domain.RequestDto;
import com.szsh.aiot.common.enums.BusinessType;
import com.szsh.aiot.hsm.dto.enu.NumberEnum;
import com.szsh.aiot.hsm.service.CameraAccessService;
import com.szsh.aiot.hsm.utils.PermitValidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @Description:智能设备及流量统计功能
 * @Date 2023/8/25: 14:06
 * @Author: LOADING>>>>>
 */

@Controller
@RequestMapping("/appSafe/flow")
public class DevicesAndTrafficStatisticsController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DevicesAndTrafficStatisticsController.class);

    @Autowired
    private PermitValidUtil permitValidUtil;

    @Resource
    private CameraAccessService cameraAccessService;

    @Autowired
    private Gson gson;

    /**
     * 5.1 通过调用此接口获取监控设备及监控设备流量数据
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @RequestMapping(value = "/deviceinfo")
    @Log(title = "摄像头提醒接口", businessType = BusinessType.OTHER)
    @CustomPermission(isValidateTyAccount = false)
    @ResponseBody
    public AjaxResult cameraAccessReminder(@RequestBody RequestDto requestDto){

        String sequenceNo = requestDto.getSequenceNo();
        JsonObject requestParams = requestDto.getRequestJsonElement().getAsJsonObject();
        String operaType = permitValidUtil.getElementValue(requestParams,"operaType");

        String broadbandNO = permitValidUtil.getElementValue(requestParams, "broadbandNO");
        String gatewayMac = permitValidUtil.getElementValue(requestParams, "gatewayMac");

        switch (operaType){
            case  NumberEnum.STR_1:
                return cameraAccessService.getFlowDevice(broadbandNO,gatewayMac);
                //获取当前监控的流量告警设备列表
            case  NumberEnum.STR_2:
                String deviceid = permitValidUtil.getElementValue(requestParams, "settingInfo");
                return cameraAccessService.getFlowPushData(deviceid);//查询流量数据接口
            case  NumberEnum.STR_3:
                return cameraAccessService.getOnlineDeviceList(broadbandNO,gatewayMac);
                //用户所有设备列表
            case  NumberEnum.STR_4:
                List<String> deviceidList = gson.fromJson(requestParams.get("settingInfo"),new TypeToken<List<String>>(){}.getType());
                return cameraAccessService.setUpFlowDevice(broadbandNO,deviceidList);
                //保存用户自定义设备列表
            default:
                AjaxResult.badRequest("未知操作，请确认后重新操作");
        }
        return AjaxResult.badRequest("未知操作，请确认后重新操作");
    }
}
